package com.changgou.goods.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.vo.BrandVo;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class BrandController {
   @Autowired
    public BrandService brandService;

   //查询全部
   @GetMapping("/findAll")
    public Result findAll(){
       List<Brand> list = brandService.findAll();
       return new Result(true, StatusCode.ACCESSERROR,"查询成功",list);
   }
   //根据id查询
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.ACCESSERROR,"查询成功",brand);
    }
   //分页查询
    @GetMapping("/findByPage/{page}/{size}")
    public Result findByPage(@PathVariable int page, @PathVariable int size){
        Page<Brand> brandPage = brandService.findByPage(page, size);
        return new Result(true, StatusCode.ACCESSERROR,"查询成功",brandPage);
    }
    //添加
    @PostMapping("/add")
    public Result add(Brand brand){
       brandService.save(brand);
       return new Result(true, StatusCode.ACCESSERROR,"添加成功");
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable int id){
       brandService.delete(id);
       return new Result(true, StatusCode.ACCESSERROR,"删除成功");
    }
    //修改
    @PutMapping("/update/{id}")
    public Result update(@PathVariable int id, Brand brand){
       brand.setId(id);
       brandService.update(brand);
       return new Result(true, StatusCode.ACCESSERROR,"修改成功");
    }
    //条件查询
    @GetMapping("/findByCondition")
    public Result findByCondition(@RequestParam Map map){
        List list = brandService.findByCondition(map);
        return new Result(true, StatusCode.ACCESSERROR,"查询成功",list);
    }
    //条件分页
    @GetMapping("/findByPageCondition/{page}/{size}")
    public Result findByPageCondition(@PathVariable int page, @PathVariable int size, @RequestParam Map map){
        Page pageCondition = brandService.findByPageCondition(map, page, size);
        return new Result(true, StatusCode.ACCESSERROR,"查询成功",pageCondition);
    }
    //根据商品分类名称查询品牌列表
    @GetMapping("/findBrandNameByCategoryName/{categoryName}")
    public Result findBrandNameByCategoryName(@PathVariable String categoryName){
        List<BrandVo> brandNameByCategoryName = brandService.findBrandNameByCategoryName(categoryName);
        return new Result(true, StatusCode.ACCESSERROR,"查询成功",brandNameByCategoryName);
    }
}
